'use strict';

var approvalConfigs = {
    delay: 500
};


/**
 * 函数节流
 * @param fn 回调
 * @param context
 * @param delay 延时
 * @param obj 参数
 */
function throttle(fn, context, delay, obj) {
    clearTimeout(fn.timeoutId);
    fn.timeoutId = setTimeout(function() {
        fn.call(context, obj);
    }, delay);
}

/**
 * 格式化当前日期
 * @param {string} [symbol="-"]
 * @param {string} [format="full"]
 * @returns
 */
function getFormatDate() {
    var symbol = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '-';
    var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'full';

    var date = new Date();
    var seperator1 = symbol;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = '0' + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    if (format == 'full') {
        return currentdate;
    } else if (format == 'year') {
        return year;
    } else {
        return year + seperator1 + month;
    }
}

function insDatetimePicker(el) {
    var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

    var options = {
        minView: 'month',
        language: 'zh-CN',
        format: 'yyyy-mm-dd',
        // weekStart: 1,
        todayBtn: true,
        autoclose: true,
        todayHighlight: true
    };
    if (!$.isEmptyObject(config)) {
        options = config;
    }
    // console.log(options);
    $(el).datetimepicker(options);
}

/**
 * 发送ajax请求，返回Promise对象
 * @param {string} url
 * @param {object} params
 * @param {string} type
 */
function sendAjax(url) {
    var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'POST';

    return new Promise(function (resolve, reject) {
        $.ajax({
            url: url,
            type: type,
            data: params,
            dataType: 'josn',
            success: function success(response) {
                resolve(response);
            },
            error: function error(_error) {
                resolve(_error);
            }
        });
    });
}

/**
 * 消息提示
 * @param {string} msg 提示内容
 * @param {number} status 0->成功 !0->失败
 * @param {string} url 跳转链接(如果不传入则刷新当前页)
 */
function notification(msg, status) {
    var url = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';

    if (!status) {
        layer.msg(msg, {icon: 1, time: 1500});
        if (url) {
            setTimeout(function () {
                location.href = url;
            }, 1500);
        } else {
            setTimeout(function () {
                location.reload();
            }, 1500);
        }
    } else {
        layer.msg(msg, {icon: 2, time: 3000});
        setTimeout(function () {
            location.href = location.replace(location.href);
        }, 3000);
    }
}

/**
 * 判断值是否在数组中
 * @param arr 数组
 * @param obj 值
 * @returns {boolean}
 */
function contains(arr, obj) {
    var i = arr.length;
    while (i--) {
        if (arr[i] == obj) {
            return true;
        }
    }
    return false;
}

function dedupe(array) {
    console.log('dedupe');
    return Array.from(new Set(array));
}

/** 数字金额大写转换(可以处理整数,小数,负数) */
function Arabia_to_Chinese (n) {
    var fraction = ['角', '分'];
    var digit = [
        '零', '壹', '贰', '叁', '肆',
        '伍', '陆', '柒', '捌', '玖'
    ];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);
    var s = '';
    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);
    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元')
        .replace(/(零.)+/g, '零')
        .replace(/^整$/, '零元整');
};

/**
 * 数字转千分位，保留小数位
 * @param num 需要转换的数字
 * @returns {string}
 */
function to_thousands(num)
{
    num = num + '';
    if (num.indexOf('.') < 0) {
        num += '.';
    }
    return num.replace(/(\d)(?=(\d{3})+\.)/g, function($0, $1) {
        return $1 + ',';
    }).replace(/\.$/, '');
}

/**
 * 格式化金额显示
 * @param $money
 * @param prefix
 * @returns {string}
 */
function moneyFormat(money, prefix = '￥')
{
    return prefix + to_thousands(to_decimal(money));
}

/**
 * 强制保留2位小数，不足补 0
 * @param x
 * @returns {*}
 */
function to_decimal(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

function moneyToNumber(moneyStr)
{
    if (!moneyStr) return 0;
    //￥9,999,999.99 保证为字符串类型避免控制报错replace方法不存在
    var str = String(moneyStr.substr(1));

    // 当进行替换之后再将字符串转换为浮点型
    var money = parseFloat(str.replace(/\,/g, ''));

    return money;
}

function getDiffYmdBetweenDate(sDate1, sDate2)
{
    var fixDate = function(sDate){
        var aD = sDate.split('-');
        for(var i = 0; i < aD.length; i++){
            aD[i] = fixZero(parseInt(aD[i]));
        }
        return aD.join('-');
    };
    var fixZero = function(n){
        return n < 10 ? '0'+n : n;
    };
    var fixInt = function(a){
        for(var i = 0; i < a.length; i++){
            a[i] = parseInt(a[i]);
        }
        return a;
    };
    var getMonthDays = function(y,m){
        var aMonthDays = [0,31,28,31,30,31,30,31,31,30,31,30,31];
        if((y%400 == 0) || (y%4==0 && y%100!=0)){
            aMonthDays[2] = 29;
        }
        return aMonthDays[m];
    };
    var checkDate = function(sDate){
    };
    var y = 0;
    var m = 0;
    var d = 0;
    var sTmp;
    var aTmp;
    sDate1 = fixDate(sDate1);
    sDate2 = fixDate(sDate2);
    if(sDate1 > sDate2){
        sTmp = sDate2;
        sDate2 = sDate1;
        sDate1 = sTmp;
    }
    var aDate1 = sDate1.split('-');
    aDate1 = fixInt(aDate1);
    var aDate2 = sDate2.split('-');
    aDate2 = fixInt(aDate2);
    //计算相差的年份
    /*aTmp = [aDate1[0]+1,fixZero(aDate1[1]),fixZero(aDate1[2])];
    while(aTmp.join('-') <= sDate2){
        y++;
        aTmp[0]++;
    }*/
    y = aDate2[0] - aDate1[0];
    if( sDate2.replace(aDate2[0],'') < sDate1.replace(aDate1[0],'')){
        y = y - 1;
    }
    //计算月份
    aTmp = [aDate1[0]+y,aDate1[1],fixZero(aDate1[2])];
    while(true){
        if(aTmp[1] == 12){
            aTmp[0]++;
            aTmp[1] = 1;
        }else{
            aTmp[1]++;
        }
        if(([aTmp[0],fixZero(aTmp[1]),aTmp[2]]).join('-') <= sDate2){
            m++;
        } else {
            break;
        }
    }
    //计算天数
    aTmp = [aDate1[0]+y,aDate1[1]+m,aDate1[2]];
    if(aTmp[1] > 12){
        aTmp[0]++;
        aTmp[1] -= 12;
    }
    while(true){
        if(aTmp[2] == getMonthDays(aTmp[0],aTmp[1])){
            aTmp[1]++;
            aTmp[2] = 1;
        } else {
            aTmp[2]++;
        }
        sTmp = ([aTmp[0],fixZero(aTmp[1]),fixZero(aTmp[2])]).join('-');
        if(sTmp <= sDate2){
            d++;
        } else {
            break;
        }
    }
    return {y:y,m:m,d:d};
}

/**
 * 渲染用户选择器
 * @param res
 * @returns {{ccDom: string, reviewersDom: string}}
 */
